/*
 * Copyright 2009 IT Mill Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package orion.jarvis.task.ui.vaadin;

import java.util.Date;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import orion.jarvis.task.Actor;
import orion.jarvis.task.Category;
import orion.jarvis.task.Desciption;
import orion.jarvis.task.Task;

import com.vaadin.Application;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class JarvisTaskApplication extends Application {
	private Window window;

	private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");

	@Override
	public void init() {
		window = new Window("My Vaadin Application");
		setMainWindow(window);
		Table table = new Table("Task");
		// set a style name, so we can style rows and cells
		table.setStyleName("iso3166");

		// size
		table.setWidth("100%");
		table.setHeight("170px");

		// selectable
		table.setSelectable(true);
		table.setMultiSelect(true);
		table.setImmediate(true); // react at once when something is selected

		// turn on column reordering and collapsing
		table.setColumnReorderingAllowed(true);
		table.setColumnCollapsingAllowed(true);
		table.addContainerProperty("id", Long.class, null, "ID", null, Table.ALIGN_CENTER);
		table.addContainerProperty("category", String.class, null, "Category", null, Table.ALIGN_CENTER);
		table.addContainerProperty("description", String.class, null, "Description", null, Table.ALIGN_CENTER);
		table.addContainerProperty("assignment", String.class, null, "Assignment", null, Table.ALIGN_CENTER);
		table.addContainerProperty("createBy", String.class, null, "Create By", null, Table.ALIGN_CENTER);
		table.addContainerProperty("createDate", Date.class, null, "Create Date", null, Table.ALIGN_CENTER);
		table.addContainerProperty("startDate", Date.class, null, "Start Date", null, Table.ALIGN_CENTER);
		table.addContainerProperty("estFinishDate", Date.class, null, "Est. Finish Date", null, Table.ALIGN_CENTER);
		table.addContainerProperty("actFinishDate", Date.class, null, "Act. Finish Date", null, Table.ALIGN_CENTER);

		// List<Task> taskList = new ArrayList<Task>();
		//		
		// taskList.add(getTask());
		// taskList.add(getTask());

		BeanItemContainer<Task> ds = new BeanItemContainer<Task>(Task.class);
		ds.addBean(getTask());
		ds.addBean(getTask());
		table.setContainerDataSource(ds);
		table.setVisibleColumns(new String[] { "id", "category", "description", "assignment", "startDate", "estFinishDate",
				"actFinishDate", "createBy", "createDate" });

		// set column headers
		// table.setColumnHeaders(new String[] { "Country", "Code", "Icon file"
		// });

		// Icons for column headers
		// table.setColumnIcon(ExampleUtil.iso3166_PROPERTY_FLAG, new
		// ThemeResource("../sampler/icons/action_save.gif"));
		// table.setColumnIcon(ExampleUtil.iso3166_PROPERTY_NAME, new
		// ThemeResource("../sampler/icons/icon_get_world.dellagif"));
		// table.setColumnIcon(ExampleUtil.iso3166_PROPERTY_SHORT, new
		// ThemeResource("../sampler/icons/page_code.gif"));

		// Column alignment
		// table.setColumnAlignment(ExampleUtil.iso3166_PROPERTY_SHORT,
		// Table.ALIGN_CENTER);

		// Column width
		// table.setColumnExpandRatio(ExampleUtil.iso3166_PROPERTY_NAME, 1);
		// table.setColumnWidth(ExampleUtil.iso3166_PROPERTY_SHORT, 70);

		// Collapse one column - the user can make it visible again
		// table.setColumnCollapsed(ExampleUtil.iso3166_PROPERTY_FLAG, true);

		// show row header w/ icon
		table.setRowHeaderMode(Table.ROW_HEADER_MODE_ICON_ONLY);
		// table.setItemIconPropertyId(ExampleUtil.iso3166_PROPERTY_FLAG);

		window.addComponent(table);

		Button button = new Button("Click Me");
		button.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				PersistenceManager pm = pmfInstance.getPersistenceManager();
				try {
					Task task1 = getTask();
					// pm.makePersistent(assignment);
					// pm.refresh(assignment);
					// pm.makePersistent(createBy);
					// pm.refresh(createBy);
					// pm.makePersistent(category);
					// pm.refresh(category);
					// pm.makePersistent(desciption);
					// pm.refresh(desciption);
					pm.makePersistent(task1);
					pm.refresh(task1);
					window.addComponent(new Label(task1.getId() + ""));
					//
					pm.flush();
				} finally {
					pm.close();
				}
				window.addComponent(new Label("Thank you for clicking"));
			}
		});
		window.addComponent(button);
	}

	private Task getTask() {
		Actor assignment = new Actor();
		assignment.setName("Asignment");
		//
		Actor createBy = new Actor();
		assignment.setName("CreateBy");
		//
		Category category = new Category();
		category.setName("Category 1");
		category.setDesc("Category 1");
		//
		Desciption desciption = new Desciption();
		desciption.setDescription("description");
		//
		Task task1 = new Task();
		task1.setActFinishDate(new Date());
		// task1.setAssignment(KeyFactory.createKey(Actor.class.getSimpleName(),
		// assignment.getId()));
		// task1.setCategory(KeyFactory.createKey(Category.class.getSimpleName(),
		// category.getId()));
		// task1.setCreateBy(KeyFactory.createKey(Actor.class.getSimpleName(),
		// createBy.getId()));
		// task1.setDescription(KeyFactory.createKey(Desciption.class.getSimpleName(),
		// desciption.getId()));
		task1.setAssignment(assignment);
		task1.setCategory(category);
		task1.setCreateBy(createBy);
		task1.setDescription(desciption);
		task1.setCreateDate(new Date());
		task1.setEstFinishDate(new Date());
		task1.setStartDate(new Date());
		return task1;
	}

}
